home *** CD-ROM | disk | FTP | other *** search
- package tetris;
-
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Form;
- import javax.microedition.lcdui.TextField;
- import javax.microedition.rms.RecordStore;
-
- public class HighScores extends Form implements CommandListener {
- static String[] names = new String[]{"", "", "", "", "", "", "", "", "", ""};
- static int[] scores = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- static TextField readName;
- static RecordStore scoresRS;
-
- public HighScores() {
- super("High Scores");
-
- try {
- this.jbInit();
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- protected void openStore() {
- try {
- scoresRS = RecordStore.openRecordStore("scores", true);
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- protected void closeStore() {
- try {
- scoresRS.closeRecordStore();
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- public void updateScores() {
- while(((Form)this).size() > 0) {
- ((Form)this).delete(0);
- }
-
- for(int i = 0; i < 10 && scores[i] > 0; ++i) {
- ((Form)this).append(String.valueOf(String.valueOf((new StringBuffer(String.valueOf(String.valueOf(i == 0 ? "" : "\n")))).append(i < 9 ? " " : "").append(i + 1).append(". ").append(names[i]).append(" [").append(scores[i]).append("]"))));
- }
-
- }
-
- private void jbInit() throws Exception {
- ((Displayable)this).setCommandListener(this);
- ((Displayable)this).addCommand(new Command("Back", 7, 1));
- this.openStore();
- readHighScores();
- this.closeStore();
- this.updateScores();
- }
-
- public void commandAction(Command command, Displayable displayable) {
- if (command.getCommandType() == 7) {
- Display.getDisplay(TetrisMIDlet.instance).setCurrent(TetrisMIDlet.mainMenu);
- }
-
- if (command.getCommandType() == 1) {
- this.addScore(TetrisMIDlet.gameScreen.gameScore, readName.getString());
- Display.getDisplay(TetrisMIDlet.instance).setCurrent(TetrisMIDlet.mainMenu);
- }
-
- }
-
- public boolean checkScore(int score) {
- return score > scores[9];
- }
-
- public void getName() {
- Form f = new Form("");
- f.append(readName = new TextField("Enter your name", "", 16, 0));
- ((Displayable)f).setCommandListener(this);
- ((Displayable)f).addCommand(new Command("OK", 1, 1));
- Display.getDisplay(TetrisMIDlet.instance).setCurrent(f);
- }
-
- public void addScore(int score, String name) {
- int i;
- for(i = 0; i < 10 && scores[i] >= score; ++i) {
- }
-
- if (i < 10) {
- for(int j = 9; j > i; --j) {
- scores[j] = scores[j - 1];
- names[j] = names[j - 1];
- }
-
- scores[i] = score;
- names[i] = name;
- }
-
- this.openStore();
- saveHighScores();
- this.closeStore();
- }
-
- public static boolean readHighScores() {
- try {
- if (scoresRS.getNumRecords() > 0) {
- byte[] b = scoresRS.getRecord(1);
- ByteArrayInputStream bais = new ByteArrayInputStream(b);
- DataInputStream dis = new DataInputStream(bais);
-
- for(int i = 0; i < 10; ++i) {
- scores[i] = dis.readInt();
- if (scores[i] == 0) {
- break;
- }
-
- int j = dis.readInt();
- char[] s = new char[j];
-
- for(int k = 0; k < j; ++k) {
- s[k] = dis.readChar();
- }
-
- names[i] = new String(s);
- }
-
- try {
- dis.close();
- bais.close();
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- boolean var9 = true;
- return var9;
- }
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- return false;
- }
-
- public static void saveHighScores() {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- DataOutputStream dos = new DataOutputStream(baos);
-
- try {
- for(int i = 0; i < 10; ++i) {
- dos.writeInt(scores[i]);
- dos.writeInt(names[i].length());
- dos.writeChars(names[i]);
- }
-
- byte[] b = baos.toByteArray();
- if (scoresRS.getNumRecords() > 0) {
- scoresRS.setRecord(1, b, 0, b.length);
- } else {
- scoresRS.addRecord(b, 0, b.length);
- }
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- } finally {
- try {
- dos.close();
- baos.close();
- } catch (Exception e) {
- ((Throwable)e).printStackTrace();
- }
-
- }
-
- }
- }
-